from app.routes import StudentRoutes
from app.views.ClasView import ClasView


class StudentView:
    def __init__(self):
        self.function_list=["查看学生信息","添加学生信息","修改学生信息","删除学生信息"]
        self.clas_view = ClasView()
    def display_menu(self):
        print("\n============== 学生成绩管理系统-学生管理 ==============")
        for idx, func in enumerate(self.function_list):
            print(f"{idx+1}.{func}")
        print("5.返回")
        print("请根据上面的选项输入数字并按回车继续...")

    def get_students(self):
        '''
        用户点击查看学生信息
        :return:
        '''
        keyword = input("请输入姓名关键字或班级关键字，回车搜索全部：")
    #     查询所有的班级信息
        resp = StudentRoutes.get_by_keyword(keyword)
        # 打印查询到的信息
        print(resp['message'])
        for data in resp["data"]:
            print(data)

    def add_student(self):
        self.clas_view.get_clases()
        clas_id = input("请输入班级id：")
        name = input("请输入学生姓名：")
        gender = input("请输入学生性别:")
        resp = StudentRoutes.add_student(clas_id, name, gender)
        print(resp['message'])

    # def update_student(self):
    #     # 先查询所有学生以便选择要修改的学生
    #     self.get_students()
    #     student_id = input("请输入要修改的学生ID：")
    #     # 查询学生当前信息
    #     resp = StudentRoutes.get_by_id(student_id)
    #     if not resp['data']:
    #         print("未找到该学生信息")
    #         return
    #     student = resp['data']
    #     print(f"当前学生信息：{student}")
    #     # 获取修改后的信息（带默认值）
    #     clas_id = input(f"请输入新班级ID[{student.clas_id}]：") or student.clas_id
    #     name = input(f"请输入新姓名[{student.name}]：") or student.name
    #     gender = input(f"请输入新性别[{student.gender}]：") or student.gender
    #     # 执行修改
    #     resp = StudentRoutes.update_student(student_id, clas_id, name, gender)
    #     print(resp['message'])

    def update_student(self):
        self.get_students()
        stu_id = input("请输入要修改的学生id：")
        new_name = input("请输入新的学生姓名，回车不修改：")
        new_gender = input("请输入新的学生性别，回车不修改:")
    #     打印班级列表
        self.clas_view.get_clases()
        new_clas_id = input("请输入新的班级id，回车不修改：")
        resp = StudentRoutes.update_student(stu_id, new_name, new_gender, new_clas_id)
        print(resp['message'])

    def delete_student(self):
        self.get_students()
        student_id = input("请输入要删除的学生ID：")
        resp = StudentRoutes.delete_student(student_id)
        print(resp['message'])
    def run(self):
        self.display_menu()
        choice = input("请输入操作符:")
        if choice == "1":
            self.get_students()
            self.run()
        elif choice == "2":
            self.add_student()
            self.run()
        elif choice == "3":
            self.update_student()
            self.run()
        elif choice == "4":
            self.delete_student()
            self.run()
        elif choice == "5":
            pass
        else:
            print("请输入正确的操作符")